﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Data;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;

namespace ZBJF.Warranty.BLL
{
    public partial class SellerInsuranceProductImagesService
    {
        public bool AddSellerInsuranceProductImages(long sellerInsuranceProductID,
            List<string> sellerInsuranceProductImagesPaths, string createUser)
        {
            var result = true;
            var sellerInsuranceProductImagesIDs = new List<long>();
            if (sellerInsuranceProductID > 0 && sellerInsuranceProductImagesPaths != null && sellerInsuranceProductImagesPaths.Any())
            {
                for (int i = 0; i < sellerInsuranceProductImagesPaths.Count; i++)
                {
                    var entity = Load(sellerInsuranceProductID, sellerInsuranceProductImagesPaths[i]);
                    if (entity == null)
                    {
                        SellerInsuranceProductImages ipImg = new SellerInsuranceProductImages();
                        long size = 0;
                        string aLength = "";
                        string absulotePath;
                        ipImg.FilePath = FileHelper.SaveBase64Image(sellerInsuranceProductImagesPaths[i], FileHelper.GetUpLoadRelativePath(), out absulotePath);

                        if (!string.IsNullOrEmpty(ipImg.FilePath) && sellerInsuranceProductImagesPaths[i].Length > 200)//Base64Image 长度肯定大于200
                        {
                            var fileName = ipImg.FilePath.Split('/').Last();
                            FileHelper.GetImageInfo(absulotePath + fileName, ref size, ref aLength);

                            ipImg.FileName = fileName;
                            ipImg.FileSize = size.ToString();
                            ipImg.CreateTime = DateTime.Now;
                            ipImg.CreateBy = createUser;
                            ipImg.IsAvailable = true;
                            ipImg.IsFirstImage = true;
                            ipImg.SellerInsuranceProductId = sellerInsuranceProductID;
                            var id = AddEntities(ipImg).ID;
                            sellerInsuranceProductImagesIDs.Add(id);
                            result = id > 0;
                        }
                    }
                    else
                    {
                        sellerInsuranceProductImagesIDs.Add(entity.ID);
                    }
                }
            }
            var tobeDeleted = LoadEntities(o =>
                        o.SellerInsuranceProductId == sellerInsuranceProductID && !sellerInsuranceProductImagesIDs.Contains(o.ID)).ToList();
            if (tobeDeleted.Any())
                tobeDeleted.ForEach(o => DeleteEntities(o));
            return result;
        }

        public SellerInsuranceProductImages Load(long sellerInsruanceProductID, string imagePath)
        {
            return LoadEntities(o => o.SellerInsuranceProductId == sellerInsruanceProductID && o.FilePath.Equals(imagePath)).FirstOrDefault();
        }

        public SellerInsuranceProductImages GetFirstImageByProductId(long productId)
        {
            return LoadEntities(o => o.SellerInsuranceProductId == productId && o.IsFirstImage).FirstOrDefault();
        }

        public IQueryable<SellerInsuranceProductImages> LoadInsuranceProductImages(long productId)
        {
            return LoadEntities(o => o.SellerInsuranceProductId == productId).OrderBy(o => o.Sort);
        }
    }
}
